﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 材料入库红票 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            int bid = 0;

            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out bid);
                }

                if (bid > 0)
                {
                    this.WuLiao.Enabled = false;
                    this.ShuLiang.Enabled = false;
                    this.DanJia.Enabled = false;

                    this.Label_ID.Text = bid.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.Label_ID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                    
                }
                else
                {
                    this.Date1.Text = DateTime.Today.ToLongDateString();
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (this.SqlDataSource1.SelectParameters["ID"].DefaultValue != "0")
            {
                if (Convert.IsDBNull(e.Command.Parameters[1].Value))
                {
                    this.Notice = "读取失败";
                    this.Button_Save.Enabled = false;
                    return;
                }
            }

            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                this.LeiBie.Value = ((int)e.Command.Parameters[1].Value).ToString();
                this.Label_LeiBie.Text = ((int)e.Command.Parameters[1].Value).ToString("D10");
            }
            if (!Convert.IsDBNull(e.Command.Parameters[2].Value))
            {
                this.GongHuo.Text = ((int)e.Command.Parameters[2].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
            {

            }
            if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
            {
                this.ShuLiang.Text = ((double)e.Command.Parameters[4].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
            {
                this.DanJia.Text = ((double)e.Command.Parameters[5].Value).ToString();
            }

            if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[6].Value).ToLongDateString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[7].Value))
            {
                this.BeiZhu.Text = (string)e.Command.Parameters[7].Value;
            }

            if (!Convert.IsDBNull(e.Command.Parameters[8].Value))
            {
                this.Label_PinMing.Text = (string)e.Command.Parameters[8].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[9].Value))
            {
                this.Label_GuiGe.Text = (string)e.Command.Parameters[9].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[10].Value))
            {
                this.Label_DanWei.Text = (string)e.Command.Parameters[10].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[11].Value))
            {

            }

            if (!Convert.IsDBNull(e.Command.Parameters[12].Value))
            {
                this.DanHao.Text = ((int)e.Command.Parameters[12].Value).ToString();
            }
        }

        protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {

        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.Label_ID.Text = ((int)e.Command.Parameters[0].Value).ToString();
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Notice = "“日期”录入有误。";
                return;
            }
            int dh;
            if (!int.TryParse(this.DanHao.Text, out dh))
            {
                this.Notice = "“单号”录入有误，请录入一个正整数。";
                return;
            }

            decimal sl;
            if (!decimal.TryParse(this.ShuLiang.Text, out sl))
            {
                this.Notice = "“数量”录入有误";
                return;
            }

            decimal dj;
            if (!decimal.TryParse(this.DanJia.Text, out dj))
            {
                this.Notice = "“数量”录入有误";
                return;
            }

            if (this.Label_ID.Text == "0")
            {
                string op = (string)this.Session["OperatorName"];
                if (op == null)
                {
                    op = "";
                }
                this.SqlDataSource1.InsertParameters["保管员"].DefaultValue = op;
                this.SqlDataSource1.InsertCommand =
@"begin transaction;
declare @结存数量 numeric(18,4);
declare @结存金额 numeric(27,10);
set @结存数量=0;
set @结存金额=0;
select top 1 @结存数量=isnull(结存数量,0),@结存金额=isnull(结存金额,0) from 材料入出库 with(tablockx) where 材料ID=@材料ID order by ID desc;
set @结存数量=@结存数量+@数量;
set @结存金额=@结存金额+@数量*@单价;
insert into 材料入出库(单据号,材料ID,单位ID,日期,数量0,数量,单价,结存数量,结存金额,备注,保管员,入出)values
(@单据号,@材料ID,@单位ID,@日期,@数量0,@数量,@单价,@结存数量,@结存金额,@备注,@保管员,2);
set @ID=@@IDENTITY;commit;";
                this.SqlDataSource1.Insert();
                if (this.Label_ID.Text == "0")
                {
                    this.Notice = "保存失败";
                }
                else
                {
                    this.WuLiao.Enabled = false;
                    this.ShuLiang.Enabled = false;
                    this.DanJia.Enabled = false;
                    this.Notice = "保存成功";
                }
            }
            else
            {
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Notice = "保存成功";
                }
                else
                {
                    this.Notice = "保存失败";
                }

            }
        }
    }
}
